import pandas as pd

res = []
child = {}
with open('or2i.txt', encoding='utf-8') as file:
    for line in file.readlines():
        question, p, c = line.strip().split('|')
        res.append([question.strip(), p.strip(), c.strip()])
        c = c.strip()
        child[c] = child.get(c, 0) + 1

child = sorted(child.items(), key=lambda x: x[1], reverse=True)
cls = 'child_label.txt'
label = {}
# label['None'] = 0
with open(cls, 'w', encoding='utf-8')as file:
    # file.write('None 0\n')
    for i, v in enumerate(child):
        file.write(v[0] + ' ' + str(i) + '\n')
        label[v[0]] = i

df = pd.DataFrame(columns=['question', 'label'])

y = []
q = []
for line in res:
    # index = 0
    # if line[1] != line[2]:
    #     index = label[line[2]]
    index = label[line[2]]
    y.append(index)
    q.append(line[0])

df['question'] = q
df['label'] = y
df.to_csv('child_data.csv', encoding='utf_8_sig', index=False)
